{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_01_2_intro_python.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# T81-558: Applications of Deep Neural Networks\n",
    "**Module 1: Python Preliminaries**\n",
    "* Instructor: [Jeff Heaton](https://sites.wustl.edu/jeffheaton/), McKelvey School of Engineering, [Washington University in St. Louis](https://engineering.wustl.edu/Programs/Pages/default.aspx)\n",
    "* For more information visit the [class website](https://sites.wustl.edu/jeffheaton/t81-558/)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Module 1 Material\n",
    "\n",
    "* Part 1.1: Course Overview [[Video]](https://www.youtube.com/watch?v=IzZSwS45vt4&list=PLjy4p-07OYzulelvJ5KVaT2pDlxivl_BN) [[Notebook]](https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_01_1_overview.ipynb)\n",
    "* **Part 1.2: Introduction to Python** [[Video]](https://www.youtube.com/watch?v=czq5d53vKvo&list=PLjy4p-07OYzulelvJ5KVaT2pDlxivl_BN) [[Notebook]](https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_01_2_intro_python.ipynb)\n",
    "* Part 1.3: Python Lists, Dictionaries, Sets and JSON [[Video]](https://www.youtube.com/watch?v=kcGx2I5akSs&list=PLjy4p-07OYzulelvJ5KVaT2pDlxivl_BN) [[Notebook]](https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_01_3_python_collections.ipynb)\n",
    "* Part 1.4: File Handling [[Video]](https://www.youtube.com/watch?v=FSuSLCMgCZc&list=PLjy4p-07OYzulelvJ5KVaT2pDlxivl_BN) [[Notebook]](https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_01_4_python_files.ipynb)\n",
    "* Part 1.5: Functions, Lambdas, and Map/Reduce [[Video]](https://www.youtube.com/watch?v=jQH1ZCSj6Ng&list=PLjy4p-07OYzulelvJ5KVaT2pDlxivl_BN) [[Notebook]](https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_01_5_python_functional.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Google CoLab Instructions\n",
    "\n",
    "The following code ensures that Google CoLab is running the correct version of TensorFlow."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: not using Google CoLab\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    from google.colab import drive\n",
    "    %tensorflow_version 2.x\n",
    "    COLAB = True\n",
    "    print(\"Note: using Google CoLab\")\n",
    "except:\n",
    "    print(\"Note: not using Google CoLab\")\n",
    "    COLAB = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Part 1.2: Introduction to Python\n",
    "\n",
    "Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.  Python has become a common language for machine learning research and is the primary language for TensorFlow. \n",
    "\n",
    "Python 3.0, released in 2008, was a significant revision of the language that is not entirely backward-compatible, and much Python 2 code does not run unmodified on Python 3.  This course makes use of Python 3.  Furthermore, TensorFlow is not compatible with versions of Python earlier than 3. A non-profit organization, the Python Software Foundation (PSF), manages and directs resources for Python development. On January 1, 2020, the PSF discontinued the Python 2 language and no longer provides security patches and other improvements. Python interpreters are available for many operating systems. \n",
    "\n",
    "The first two modules of this course provide an introduction to some aspects of the Python programming language.  However, entire books focus on Python.  Two modules will not cover every detail of this language.  The reader is encouraged to consult additional sources on the Python language.\n",
    "\n",
    "Like most tutorials, we will begin by printing Hello World."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello World\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above code passes a constant string, containing the text \"hello world\" to a function that is named print.  \n",
    "\n",
    "You can also leave comments in your code to explain what you are doing.  Comments can begin anywhere in a line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World\n"
     ]
    }
   ],
   "source": [
    "# Single line comment (this has no effect on your program)\n",
    "print(\"Hello World\") # Say hello"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Strings are very versatile and allow your program to process textual information.  Constant string, enclosed in quotes, define literal string values inside your program. Sometimes you may wish to define a larger amount of literal text inside of your program.  This text might consist of multiple lines. The triple quote allows for multiple lines of text."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Print\n",
      "Multiple\n",
      "Lines\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\"\"\"Print\n",
    "Multiple\n",
    "Lines\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Like many languages Python uses single (') and double (\") quotes interchangeably to denote literal string constants. The general convention is that double quotes should enclose actual text, such as words or sentences.  Single quotes should enclose symbolic text, such as error codes.  An example of an error code might be 'HTTP404'. \n",
    "\n",
    "However, there is no difference between single and double quotes in Python, and you may use whichever you like.  The following code makes use of a single quote."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World\n"
     ]
    }
   ],
   "source": [
    "print('Hello World')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition to strings, Python allows numbers as literal constants in programs. Python includes support for floating-point, integer, complex, and other types of numbers.  This course will not make use of complex numbers.  Unlike strings, quotes do not enclose numbers. \n",
    "\n",
    "The presence of a decimal point differentiates floating-point and integer numbers.  For example, the value 42 is an integer. Similarly,  42.5 is a floating-point number. If you wish to have a floating-point number, without a fraction part, you should specify a zero fraction.  The value 42.0 is a floating-point number, although it has no fractional part. As an example, the following code prints two numbers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n",
      "42.5\n"
     ]
    }
   ],
   "source": [
    "print(42)\n",
    "print(42.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So far, we have only seen how to define literal numeric and string values.  These literal values are constant and do not change as your program runs.  Variables allow your program to hold values that can change as the program runs.  Variables have names that allow you to reference their values. The following code assigns an integer value to a variable named \"a\" and a string value to a variable named \"b.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "ten\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = \"ten\"\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The key feature of variables is that they can change.  The following code demonstrates how to change the values held by variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "print(a)\n",
    "a = a + 1\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can mix strings and variables for printing.  This technique is called a formatted or interpolated string.  The variables must be inside of the curly braces.  In Python, this type of string is generally called an f-string.  The f-string is denoted by placing an \"f\" just in front of the opening single or double quote that begins the string.  The following code demonstrates the use of an f-string to mix several variables with a literal string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of a is 10\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "print(f'The value of a is {a}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also use f-strings with math (called an expression).  Curly braces can enclose any valid Python expression for printing.  The following code demonstrates the use of an expression inside of the curly braces of an f-string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of a plus 5 is 15\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "print(f'The value of a plus 5 is {a+5}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python has many ways to print numbers; these are all correct.  However, for this course, we will use f-strings. The following code demonstrates some of the varied methods of printing numbers in Python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a is 5\n",
      "a is 5\n",
      "a is 5\n",
      "a is 5\n"
     ]
    }
   ],
   "source": [
    "a = 5\n",
    "\n",
    "print(f'a is {a}') # Preferred method for this course.\n",
    "print('a is {}'.format(a))\n",
    "print('a is ' + str(a))\n",
    "print('a is %d' % (a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can use if-statements to perform logic.  Notice the indents?  These if-statements are how Python defines blocks of code to execute together.  A block usually begins after a colon and includes any lines at the same level of indent. Unlike many other programming languages, Python uses whitespace to define blocks of code.  The fact that whitespace is significant to the meaning of program code is a frequent source of annoyance for new programmers of Python.  Tabs and spaces are both used to define the scope in a Python program.  Mixing both spaces and tabs in the same program is not recommended.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The variable a is not greater than 5\n"
     ]
    }
   ],
   "source": [
    "a = 5\n",
    "if a>5:\n",
    "    print('The variable a is greater than 5.')\n",
    "else:\n",
    "    print('The variable a is not greater than 5')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following if-statement has multiple levels.  It can be easy to indent these levels improperly, so be careful.  This code contains a nested if-statement under the first \"a==5\" if-statement.  Only if a is equal to 5 will the nested \"b==6\" if-statement be executed. Also, note that the \"elif\" command means \"else if.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The variable a is 5\n",
      "The variable b is also 6\n"
     ]
    }
   ],
   "source": [
    "a = 5\n",
    "b = 6\n",
    "\n",
    "if a==5:\n",
    "    print('The variable a is 5')\n",
    "    if b==6:\n",
    "        print('The variable b is also 6')\n",
    "elif a==6:\n",
    "    print('The variable a is 6')\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "It is also important to note that the double equal (\"==\") operator is used to test the equality of two expressions.  The single equal (\"=\") operator is only used to assign values to variables in Python.  The greater than (\">\"), less than (\"<\"), greater than or equal (\">=\"), less than or equal (\"<=\") all perform as would generally be accepted.  Testing for inequality is performed with the not equal (\"!=\") operator. \n",
    "\n",
    "It is common in programming languages to loop over a range of numbers.  Python accomplishes this through the use of the **range** operation.  Here you can see a **for** loop and a **range** operation that causes the program to loop between 1 and 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "for x in range(1, 3):  # If you ever see xrange, you are in Python 2\n",
    "    print(x)  \n",
    "# If you ever see print x (no parenthesis), you are in Python 2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This code illustrates some incompatibilities between Python 2 and Python 3.  Before Python 3, it was acceptable to leave the parentheses off of a *print* function call.  This method of invoking the *print* command is no longer allowed in Python 3.  Similarly, it used to be a performance improvement to use the *xrange* command in place of range command at times.  Python 3 incorporated all of the functionality of the *xrange* Python 2 command into the normal *range* command.  As a result, the programmer should not use the *xrange* command in Python 3.  If you see either of these constructs used in example code, then you are looking at an older Python 2 era example.\n",
    "\n",
    "The *range* command is used in conjunction with loops to pass over a specific range of numbers.  Cases, where you must loop over specific number ranges, are somewhat uncommon. Generally, programmers use loops on collections of items, rather than hard-coding numeric values into your code.  Collections, as well as the operations that loops can perform on them, is covered later in this module.\n",
    "\n",
    "The following is a further example of a looped printing of strings and numbers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding 1, sum so far is 1\n",
      "Adding 2, sum so far is 3\n",
      "Final sum: 3\n"
     ]
    }
   ],
   "source": [
    "acc = 0\n",
    "for x in range(1, 3):\n",
    "    acc += x\n",
    "    print(f\"Adding {x}, sum so far is {acc}\")\n",
    "\n",
    "print(f\"Final sum: {acc}\")"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3.9 (tensorflow)",
   "language": "python",
   "name": "tensorflow"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
